1 /*
2 * The Apache Software License, Version 1.1
3 *
4 * Copyright (c) 2002 The Apache Software Foundation. All rights
5 * reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 *
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 *
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in
16 * the documentation and/or other materials provided with the
17 * distribution.
18 *
19 * 3. The end-user documentation included with the redistribution,
20 * if any, must include the following acknowledgment:
21 * "This product includes software developed by the
22 * Apache Software Foundation (http://www.apache.org/)."
23 * Alternately, this acknowledgment may appear in the software itself,
24 * if and wherever such third-party acknowledgments normally appear.
25 *
26 * 4. The names "Apache" and "Apache Software Foundation" must
27 * not be used to endorse or promote products derived from this
28 * software without prior written permission. For written
29 * permission, please contact apache@apache.org.
30 *
31 * 5. Products derived from this software may not be called "Apache",
32 * nor may "Apache" appear in their name, without prior written
33 * permission of the Apache Software Foundation.
34 *
35 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
36 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
37 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
38 * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
39 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
40 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
41 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
42 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
43 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
44 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
45 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
46 * SUCH DAMAGE.
47 * ====================================================================
48 *
49 * This software consists of voluntary contributions made by many
50 * individuals on behalf of the Apache Software Foundation. For more
51 * information on the Apache Software Foundation, please see
52 * <http://www.apache.org/>.
53 */
54 package net.sf.cglib.proxy;
55
56 import net.sf.cglib.CodeGenTestCase;
57 import java.lang.reflect.Method;
58 import java.util.HashMap;
59 import java.util.Map;
60
61 import net.sf.cglib.proxysample.ProxySampleInterface_ReturnsBasic;
62
63 import junit.framework.*;
64
65 /***
66 * @author Chris Nokleberg <a href="mailto:chris@nokleberg.com">chris@nokleberg.com</a>
67 * @version $Id: TestProxy.java,v 1.3 2003/12/02 21:59:02 herbyderby Exp $
68 */
69 public class TestProxy extends CodeGenTestCase {
70
71 private class SimpleInvocationHandler implements InvocationHandler {
72 Object o = null;
73 public SimpleInvocationHandler(Object o) {
74 this.o = o;
75 }
76 public Object invoke(Object proxy, Method m, Object[] args) throws Throwable {
77 System.out.println("invoking " + m + " on " + o + " with " + args);
78 Object r = m.invoke(o, args);
79 System.out.println("done: " + m + " on " + o + " with " + args + ", result is " + r);
80 return r;
81 }
82 }
83
84 public void testGetProxyClassAndConstructor() throws Exception {
85 HashMap map = new HashMap();
86 map.put("test", "test");
87 InvocationHandler handler = new SimpleInvocationHandler(map);
88 Class proxyClass = Proxy.getProxyClass(TestProxy.class.getClassLoader(), new Class[] { Map.class });
89 Map proxyMap = (Map) proxyClass.getConstructor(new Class[] { InvocationHandler.class }).
90 newInstance(new Object[] { handler });
91 assertEquals("proxy delegation not correct",
92 map.get("test"), proxyMap.get("test"));
93 }
94
95 public void testGetProxyInstance() throws Exception {
96 HashMap map = new HashMap();
97 map.put("test", "test");
98 InvocationHandler handler = new SimpleInvocationHandler(map);
99 Map proxyMap = (Map) Proxy.newProxyInstance(TestProxy.class.getClassLoader(), new Class[] { Map.class }, handler);
100 assertEquals("proxy delegation not correct", map.get("test"), proxyMap.get("test"));
101 }
102
103 public void testIsProxyClass() throws Exception {
104 HashMap map = new HashMap();
105 map.put("test", "test");
106 InvocationHandler handler = new SimpleInvocationHandler(map);
107 Map proxyMap = (Map) Proxy.newProxyInstance(TestProxy.class.getClassLoader(), new Class[] { Map.class }, handler);
108 assertTrue("real proxy not accepted", Proxy.isProxyClass(proxyMap.getClass()));
109 }
110
111 private class FakeProxy extends Proxy {
112 public FakeProxy(InvocationHandler ih) {
113 super(ih);
114 }
115 }
116
117 public void testIsNotProxyClass() throws Exception {
118 assertTrue("fake proxy accepted as real",
119 !Proxy.isProxyClass(FakeProxy.class));
120 }
121
122 private static class ReturnNullHandler implements InvocationHandler {
123 public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
124 return null;
125 }
126 }
127
128 public void testReturnNull() throws Exception {
129 System.err.println("hello");
130 ProxySampleInterface_ReturnsBasic rb =
131 (ProxySampleInterface_ReturnsBasic)
132 Proxy.newProxyInstance(null,
133 new Class[]{ ProxySampleInterface_ReturnsBasic.class },
134 new ReturnNullHandler());
135 try {
136 int result = rb.getKala(11);
137 fail("must throw an exception, but returned " + result);
138 } catch (NullPointerException ignore) { }
139 }
140
141 public void testGetInvocationHandler() throws Exception {
142 HashMap map = new HashMap();
143 map.put("test", "test");
144 InvocationHandler handler = new InvocationHandler() {
145 public Object invoke(Object o, Method method, Object[] args) throws Exception {
146 throw new Exception("test!");
147 }
148 };
149 Map proxyMap = (Map) Proxy.newProxyInstance(TestProxy.class.getClassLoader(), new Class[] { Map.class }, handler);
150 assertSame("should be the same handler", handler, Proxy.getInvocationHandler(proxyMap));
151 }
152
153 public void testException() throws Exception {
154 HashMap map = new HashMap();
155 map.put("test", "test");
156 InvocationHandler handler = new InvocationHandler() {
157 public Object invoke(Object o, Method method, Object[] args) throws Exception {
158 throw new Exception("test!");
159 }
160 };
161 Map proxyMap = (Map) Proxy.newProxyInstance(TestProxy.class.getClassLoader(), new Class[] { Map.class }, handler);
162 try {
163 proxyMap.get("test"); //should throw exception
164 fail("proxy exception handling not correct, should throw exception");
165 } catch (UndeclaredThrowableException e) {
166 System.out.println("exception: " + e);
167 } catch (Exception e) {
168 fail("proxy exception handling not correct, threw wrong exception: " + e);
169 }
170 }
171
172 public void testEquals() throws Exception {
173 final Object k1 = new Object();
174 final Object k2 = new Object();
175 InvocationHandler handler = new InvocationHandler() {
176 public Object invoke(Object o, Method method, Object[] args) throws Exception {
177 if (method.getName().equals("equals")) {
178 return (args[0] == k1) ? Boolean.TRUE : Boolean.FALSE;
179 }
180 return null;
181 }
182 };
183 Object proxy = Proxy.newProxyInstance(TestProxy.class.getClassLoader(), new Class[] { Map.class }, handler);
184 assertTrue(proxy.equals(k1));
185 assertTrue(!proxy.equals(k2));
186 }
187
188 public TestProxy(String testName) {
189 super(testName);
190 }
191
192 public static void main(String[] args) {
193 junit.textui.TestRunner.run(suite());
194 }
195
196 public static Test suite() {
197 return new TestSuite(TestProxy.class);
198 }
199 }
This page was automatically generated by Maven